package com.yh.homestay.service.impl;

import com.yh.homestay.common.HomeStayResult;
import com.yh.homestay.dao.*;
import com.yh.homestay.pojo.SysPermission;
import com.yh.homestay.pojo.SysRolePermission;
import com.yh.homestay.pojo.SysStaffRole;
import com.yh.homestay.service.SysPermissionService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName SysPermissionServiceImpl
 * @Description TODO
 * @Author GCL
 * @Date 2020/1/6 15:17
 */
@Service
public class SysPermissionServiceImpl implements SysPermissionService {

    private Log logger= LogFactory.getLog(SysPermissionServiceImpl.class);

    @Autowired
    private SysPermissionMapper sysPermssionMapper;

    @Autowired
    private SysStaffRoleMapper sysStaffRoleMapper;

    @Autowired
    private SysRolePermissionMapper sysRolePermissionMapper;

    @Autowired
    private SysPermissionMapper sysPermissionMapper;

    /**
    * @Date: 2020/1/9 20:47
    * @Description: 模糊匹配一个或多个关键字的url
    * @param elements
    * @return: java.lang.String[]
    */
    @Override
    public String[] getManyInUrls(String... elements) {
        List<String> urls = new ArrayList<>();
        for (int i = 0; i < elements.length; i++) {
            Example example = new Example(SysPermission.class);
            example.createCriteria().andLike("perUrl", elements[i]);
            List<SysPermission> sysPermissions = sysPermssionMapper.selectByExample(example);
            for (SysPermission sysPermission : sysPermissions) {
                urls.add(sysPermission.getPerUrl());
            }
        }
        return urls.toArray(new String[urls.size()]);
    }

    /**
    * @Date: 2020/1/10 9:49
    * @Description: 员工permission信息展示
    * @param request
    * @return: java.util.List<com.yh.homestay.pojo.SysPermission>
    */
    @Override
    public HomeStayResult showPermission(HttpServletRequest request) {
        String staffId1 = request.getSession().getAttribute("staff").toString();
        int staffId = Integer.parseInt(staffId1);
        Example example = new Example(SysStaffRole.class);
        example.createCriteria().andEqualTo("staffId",staffId);
        List<SysStaffRole> sysStaffRoles = sysStaffRoleMapper.selectByExample(example);
        List<SysPermission> sysPermissions = new ArrayList<SysPermission>();
        try {
            for (SysStaffRole sysStaffRole:sysStaffRoles) {
                Integer roleId = sysStaffRole.getRoleId();
                Example example1 = new Example(SysRolePermission.class);
                example1.createCriteria().andEqualTo("roleId",roleId);
                List<SysRolePermission> sysRolePermissions = sysRolePermissionMapper.selectByExample(example1);
                for (SysRolePermission sysRolePermission:sysRolePermissions) {
                    Integer perId = sysRolePermission.getPerId();
                    SysPermission sysPermission = sysPermissionMapper.selectByPrimaryKey(perId);
                    sysPermissions.add(sysPermission);
                }
            }
            return new HomeStayResult(0,"success",sysStaffRoles.size(),sysPermissions);
        }catch (Exception e){
            logger.debug(e);
            return HomeStayResult.build(500,"error");
        }
    }

    @Override
    public HomeStayResult updatePermission(SysPermission sysPermission) {
        try{
            sysPermissionMapper.updateByPrimaryKeySelective(sysPermission);
            return HomeStayResult.ok();
        }catch (Exception e){
            e.printStackTrace();
            return HomeStayResult.build(500,"error");
        }
    }

    @Override
    public HomeStayResult addPermission(SysPermission sysPermission) {
        sysPermissionMapper.insertSelective(sysPermission);

        return null;
    }
}
